package com.boot.until;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * ftp服务器上传
 */
@Data
@Slf4j
public class FtpUntil {
    //常量
    public static final String FTP_IP="39.106.29.145";
    public static final String FTP_USERNAME="ftpjun";
    public static final String FTP_PASSWORD="china2017";
    //属性
    private String ip;
    private Integer port;
    private String username;
    private String password;
    public static FTPClient ftpClient;

    public FtpUntil(String ip , Integer port , String username , String password ) {
        this.ip = ip;
        this.port = port;
        this.username = username;
        this.password = password;
    }

    /**
     * 批量上传文件
     * @param fileList
     * @return
     */
    public static Boolean uploadFile(List<File> fileList){
        FtpUntil ftpUntil=new FtpUntil ( FTP_IP,21,FTP_USERNAME,FTP_PASSWORD );
        log.info ( "开始连接FTP服务器" );
        Boolean is_success=uploadFile ("ftpimg", fileList );
        log.info ( "上传结束，结果={}",is_success );
        return is_success;
    }

    /**
     * 上传图片到ftp服务器
     * @param remotePath
     * @param fileList
     * @return
     */
    private static Boolean uploadFile(String remotePath , List<File> fileList){
        Boolean upload=true;
        FileInputStream inputStream=null;
        //连接Ftp服务器
        if(connectService ( FTP_IP,21,FTP_USERNAME,FTP_PASSWORD )){
            try {
                ftpClient.changeWorkingDirectory (remotePath);
                ftpClient.setBufferSize ( 1024 );
                ftpClient.setControlEncoding ( "UTF-8" );
                ftpClient.enterLocalPassiveMode();
                ftpClient.setFileType ( FTPClient.BINARY_FILE_TYPE );
                ftpClient.enterLocalActiveMode ();
                for (File file : fileList) {
                    inputStream=new FileInputStream ( file );
                    ftpClient.storeFile ( file.getName (),inputStream);
                }
            } catch (IOException e) {
                log.error ( "【上传文件异常】={}",e );
                upload=false;
            }finally {
                try {
                    inputStream.close ();
                    ftpClient.disconnect ();
                } catch (IOException e) {
                    e.printStackTrace ();
                }
            }
        }
        return upload;
    }

    /**
     * 连接FTP服务器
     * @param port
     * @param username
     * @param password
     * @return
     */
    private static Boolean connectService(String ip , int port , String username , String password){
        ftpClient =new FTPClient ();
        Boolean isSuccess=false;
        try {
            ftpClient.connect ( FTP_IP );
            isSuccess= ftpClient.login ( username,password );
        } catch (IOException e) {
            log.error ( "【Ftp服务器连接异常】={}",e );
        }
        return isSuccess;
    }
}
